#include "TestSearchEngine.h"

#include <QDebug>
#include <QFile>
#include <QJsonDocument>
#include <QJsonObject>
#include <QLibrary>
#include <QRandomGenerator>
#include <string>
#include <thread>

void TestSearchEngine::search(int serialNum, const QString& key) {
  s_result = new std::string[300];

  std::thread t(std::bind(&TestSearchEngine::doSearch, this, serialNum));
  t.join();
}

void TestSearchEngine::setResultCallback(
    std::function<void(int, QJsonArray)> fun) {
  m_fun = fun;
}

void TestSearchEngine::doSearch(int seriaNum) {
  int count = QRandomGenerator::global()->bounded(200);
  count = std::max(3, count);
  QJsonArray array;
  for (int i = 0; i < count; ++i) {
    QJsonArray arr;
    arr.push_back(QString::number(i));
    arr.push_back("第二列");
    arr.push_back("第三列 第三列 第三列 第三列");
    arr.push_back("第四列");
    array.push_back(arr);
  }
  if (m_fun) m_fun(seriaNum, array);
}

QString str2qstr(const std::string str) {
  return QString::fromLocal8Bit(str.data());
}